package com.lzw.breezeNovel.WebAdmin.service.impl;

import com.lzw.breezeNovel.WebAdmin.mapper.ChapterMapper;
import com.lzw.breezeNovel.WebAdmin.service.ChapterService;
import com.lzw.breezeNovel.model.entity.pojo.novel.Chapter;
import jakarta.annotation.Resource;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
@SuppressWarnings("all")
public class ChapterServiceImpl implements ChapterService {

    /**
     * 注入mapper
     * @param id
     */
    @Resource
    private ChapterMapper chapterMapper;


    /**
     * 删除章节
     * @param id
     */
    @Override
    public void deleteChapter(Long id) {
        chapterMapper.deleteById(id);
    }

    /**
     * 新增章节
     * @param chapter
     */
    public void addChapter(@NotNull Chapter chapter) {
        //获取内容统计字数
        int wordCount = chapter.getContent().length();
        chapter.setCount(wordCount);
        Date date = new Date();
        chapter.setCreateTime(date);
        chapter.setUpdateTime(date);
        chapterMapper.insert(chapter);
    }

    /**
     * 修改章节
     * @param chapter
     */
    @Override
    public void updateChapter(Chapter chapter) {
        chapterMapper.updateById(chapter);
    }
}
